package com.hsav.kcxb.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hsav.kcxb.R;
import com.hsav.kcxb.adapter.SingerListAdapter;
import com.kcbsdk.EventHandler;
import com.kcbsdk.MyRankBa.AObbligStyle;
import com.kcbsdk.MyRankBa.AObbligatoList;
import com.kcbsdk.MyTools.ACloudType;
import com.kcbsdk.MyTools.AIconType;
import com.kcbsdk.MyTools.AIconTypeList;

import java.util.ArrayList;

import main.MAPI;

import static com.hsav.kcxb.main.MSYS.mMyRankBa;
import static com.hsav.kcxb.main.MSYS.mMyTools;

/**
 * Created by zhao on 2017/2/12.
 */

public class SingerListActivity extends BaseActivity {

    private RecyclerView mSingerRecycler;
    private ImageView mLeftImg;
    private TextView mTitleTx;
    private ArrayList<AIconTypeList> singerlist;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aty_singerlist_layout);
        initKcbData();
        initView();
    }

    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        setContentView(R.layout.aty_singerlist_layout);
        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        singerlist = null;
    }

    private void initKcbData() {          // 数据接口回调函数
        if (mMyTools != null){
            mMyTools.setUiEventCallBack(this, new EventHandler() {
                @Override
                public void onComplete(int methodType, boolean isFail, String message) {
                    if (methodType > 0){
                        if (isFail){
                            mMyTools.Toast(String.format("%s%s(%08x)", getString(R.string.methodError), message, methodType));
                        }
                        else {
                            if (mMyTools.isSucessIconTypeListDbs(methodType, AIconType.ICON_OBB_ARTIST)){
                                String gsLocal_1 = "歌手列表：";
                                if (null != mMyTools.mAIconTypeList) {
                                    mMyTools.Log(String.format("%d ", mMyTools.mAIconTypeList.length));
                                    singerlist = new ArrayList<AIconTypeList>();
                                    for (int counter = 0; counter < mMyTools.mAIconTypeList.length; counter++) {
                                        singerlist.add(mMyTools.mAIconTypeList[counter]);

                                        gsLocal_1 += String.format(" %s ", mMyTools.mAIconTypeList[counter].TypeName);
                                        gsLocal_1 += String.format("%s(%d)", mMyTools.mAIconTypeList[counter].IconFile, MAPI.GET_FILE_LENGTH(mMyTools.mAIconTypeList[counter].IconFile));
                                    }
                                    mMyTools.Log(gsLocal_1);

                                    SingerListAdapter singerListAdapter = new SingerListAdapter(SingerListActivity.this, singerlist);
                                    mSingerRecycler.setAdapter(singerListAdapter);
                                }
                            }
                        }
                    } else{
                    }
                }
            });
            mMyTools.fetchIconTypeList(ACloudType.TYPE_KBA, AIconType.ICON_OBB_ARTIST);
        }
        mMyRankBa.setUiEventCallBack(this, new EventHandler() {
            @Override
            public void onComplete(int methodType, boolean isFail, String message) {
                if (methodType > 0){
                    if (isFail){
                        mMyRankBa.Toast(String.format("%s%s(%08x)", getString(R.string.methodError), message, methodType));
                    }
                    else {
                    }
                } else {
                    if (mMyRankBa.isEventDownImgSmall()){

//                        if (null != mMyTools.mAIconTypeList) {
//                            String gsLocal_1 = "图片事件：";
//                            for (int counter = 0; counter < mMyTools.mAIconTypeList.length; counter++) {
//                                gsLocal_1 += String.format("%s(%d) ", mMyTools.mAIconTypeList[counter].IconFile, MAPI.GET_FILE_LENGTH(mMyTools.mAIconTypeList[counter].IconFile));
//                            }
//                            mMyTools.Log(gsLocal_1);
//                        }
                        if (singerlist != null)
                        {
                            SingerListAdapter singerListAdapter = new SingerListAdapter(SingerListActivity.this, singerlist);
                            mSingerRecycler.setAdapter(singerListAdapter);
                        }
                    }
                }
            }
        });
    }

    private void initView() {
        mLeftImg = (ImageView)findViewById(R.id.iv_top_back);
        mLeftImg.setVisibility(View.VISIBLE);
        mLeftImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTitleTx = (TextView)findViewById(R.id.tv_top_title);
        mTitleTx.setText("歌星");

        mSingerRecycler = (RecyclerView)findViewById(R.id.recycler_sing_list);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,5,GridLayoutManager.VERTICAL,false);
        mSingerRecycler.setLayoutManager(gridLayoutManager);

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SingerListActivity.this, SingerListMoreActivity.class);
                intent.putExtra(SingerListMoreActivity.KEY_SINGER_GROUP_NAME, (String) v.getTag());
                startActivity(intent);
            }
        };
        View view = findViewById(R.id.view_male_star_zh);
        view.setTag(SingerListMoreActivity.SINGER_GROUPS[0]);
        view.setOnClickListener(listener);

        view = findViewById(R.id.view_female_star_zh);
        view.setTag(SingerListMoreActivity.SINGER_GROUPS[1]);
        view.setOnClickListener(listener);

        view = findViewById(R.id.view_group_zh);
        view.setTag(SingerListMoreActivity.SINGER_GROUPS[2]);
        view.setOnClickListener(listener);

        view = findViewById(R.id.view_male_star_yy);
        view.setTag(SingerListMoreActivity.SINGER_GROUPS[3]);
        view.setOnClickListener(listener);

        view = findViewById(R.id.view_female_star_yy);
        view.setTag(SingerListMoreActivity.SINGER_GROUPS[4]);
        view.setOnClickListener(listener);

        view = findViewById(R.id.view_group_yy);
        view.setTag(SingerListMoreActivity.SINGER_GROUPS[5]);
        view.setOnClickListener(listener);

        view = findViewById(R.id.view_male_star_other);
        view.setTag(SingerListMoreActivity.SINGER_GROUPS[6]);
        view.setOnClickListener(listener);

        view = findViewById(R.id.view_female_star_other);
        view.setTag(SingerListMoreActivity.SINGER_GROUPS[7]);
        view.setOnClickListener(listener);

        view = findViewById(R.id.view_group_other);
        view.setTag(SingerListMoreActivity.SINGER_GROUPS[8]);
        view.setOnClickListener(listener);
    }


}
